package com.mf.utils;

import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lmf
 * @Create: 2025/4/21 8:56
 * @module
 */
public class UtilConverter {

    /**
     * 将实体类转成Bo
     */
    public static <T,S> T convert(S source,Class<T> targetClass){
        try {
            final T target = targetClass.getDeclaredConstructor().newInstance();
            BeanUtils.copyProperties(source, target);
            return target;
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException(e);
        }

    }

    public static <T, S> List<T> converts(S[] source, Class<T> targetClass) {
        // 创建一个数组，用于存储转换后的对象
        List<T> target = new ArrayList<>();
        for (S s : source) {
            T bo = convert(s, targetClass);
            target.add(bo);
        }
        return target;
    }

}
